﻿using Common.Utily;
using Data.Video;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace Crawler.Controllers
{
    public class HomeController : Controller
    {
        private VideoEntities db = new VideoEntities();
        public ActionResult Index()
        { 
            return View();
        }
        public ActionResult Haivl_TV()
        {
            //string html = "";
            //string url = "http://haivl.tv/new/";
            //for (int i = 1; i <= 980; i++)
            //{
            //    WebClient client = new WebClient();
            //    client.Encoding = System.Text.Encoding.UTF8;
            //    try
            //    {
            //        addVideo(client.DownloadString(url + i));
            //    }
            //    catch (Exception ex)
            //    {
            //        ex = ex;
            //    }
            //}
            //ViewBag.html = html;
            return PartialView();
        }
        Model.Crawler c = new Model.Crawler();
        public IList<Video> GetObjectBySlugLike(string slug)
        {
            return db.Video.Where(x => x.Slug.Contains(slug)).ToList();
        }
        void addVideo(string html) {
            html = c.GetInnerHtml(html, ".videoList",false);

          HtmlDocument htmlDoc = new HtmlDocument();
          htmlDoc.OptionFixNestedTags = true;
          htmlDoc.LoadHtml(html);            
          foreach (HtmlNode node in htmlDoc.DocumentNode.SelectNodes("//div[@class='videoListItem']"))
          {
              try
              {
                  Video v = new Video();
                  v.CateId = 0;
                  v.Description = "";
                  v.Slug = "";


                  foreach (HtmlNode node2 in node.SelectNodes(".//span[@class]"))
                  {
                      string attributeValue = node2.GetAttributeValue("class", "");
                      if (attributeValue == "views")
                      {
                          int temp = 0;
                          int.TryParse(node2.InnerText.Replace(".", ""), out temp);
                          v.View = temp;
                      }
                      if (attributeValue == "comments")
                      {
                          int Comment = 0;
                          int.TryParse(node2.InnerText.Replace(".", ""), out Comment);
                          v.Comment = Comment;
                      }
                  }
                  HtmlNode nodes = node.SelectNodes(".//a[@data-id]").FirstOrDefault();

                  int data_id = 0;
                  int.TryParse(nodes.GetAttributeValue("data-id", ""), out data_id);
                  v.IdOld = data_id;

                  v.YCode = nodes.GetAttributeValue("data-youtubeid", "");

                  v.Title = HttpUtility.HtmlDecode(node.SelectNodes(".//img[@alt]").FirstOrDefault().GetAttributeValue("alt", ""));

                  foreach (HtmlNode node2 in node.SelectNodes(".//div[@class]"))
                  {
                      string attributeValue = node2.GetAttributeValue("class", "");
                      if (attributeValue == "duration")
                      {
                          v.TotalTime = attributeValue;
                      }
                  }
                  v.Slug = Strings.GenerateSlug(v.Title, x => GetObjectBySlugLike(StringUtils.CreateUrl(v.Title, "-")));
                  db.Video.Add(v);
                  db.SaveChanges();
              }catch(Exception ex){
                  ex = ex;
              }

          } 
        }
    }
}
 
 
//        <a class="play" data-id="10673" data-youtubeid="iwtAO6XfxZs" href="/video/10673" target="_blank">
//            <img src="http://img.youtube.com/vi/iwtAO6XfxZs/sddefault.jpg" width="728" alt="Những pha ngẫu hứng mộng mơ của anh xe tải :))" />
//            <span class="playIcon"></span>
//            <div class="duration">
//                2:24
//            </div>
//        </a>
 